﻿using AntdUI;
using LiveChartsCore.VisualElements;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TCOChart
{
    public partial class LayoutForm : Form
    {
        public LayoutForm()
        {
            InitializeComponent();
            InitSelectItems();
            InitData();
        }
        private void InitData()
        {
            select_align.SelectedIndex = 0;
            switch_autoscroll.Checked = false;
        }


        private void InitSelectItems()
        {
            //初始化方向
            select_align.Items.Clear();
            foreach (TAlignFlow align in Enum.GetValues(typeof(TAlignFlow)))
            {
                select_align.Items.Add(align);
            }
        }

        private void switch_autoscroll_CheckedChanged(object sender, BoolEventArgs e)
        {
            if (flowPanel1.InvokeRequired)
            {
                flowPanel1.Invoke(new Action(() => { flowPanel1.AutoScroll = switch_autoscroll.Checked; }));
            }
            else
            {
                flowPanel1.AutoScroll = switch_autoscroll.Checked;
            }
        }

        private void select_align_SelectedIndexChanged(object sender, IntEventArgs e)
        {
            if (select_align.InvokeRequired)
            {
                select_align.Invoke(new Action(() => { flowPanel1.Align = (TAlignFlow)select_align.SelectedValue; }));
            }
            else
            {
                flowPanel1.Align = (TAlignFlow)select_align.SelectedValue;
            }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            var btn = new AntdUI.Button() { 
                Text = "btn" + (flowPanel1.Controls.Count + 1),
                Type = TTypeMini.Primary,
                //Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                Size = new Size(90, 42),
            };
            flowPanel1.Controls.Add(btn);
            //btn.BringToFront();
        }

        private void button_del_Click(object sender, EventArgs e)
        {
            if(flowPanel1.Controls.Count > 0)
            {
                flowPanel1.Controls.RemoveAt(0);
            }
        }

        /// <summary>
        /// spantext的格式为; or 回车 分割行，空格 分割列
        /// 例如 50% 50%;20% 80%;150 200;-20% 30% 50%
        /// 50% 50%;20% 80%;150 200; 表示行宽值 
        /// 50% 50%;表示第一行有两列，行宽占比为50% 50%
        /// -20% 30% 50%  这里的"-"是一个分隔符，“-”前面的是设置行宽值，后面的表示行高值
        /// 20% 30% 50% 表示有三行，三行的行高占比各为20% 30% 50%
        /// 这个span的格式是固定的，gridPanel.Span会自动解析这个格式进行布局
        /// </summary>
        /// <param name="spantext"></param>
        /// <param name="gap"></param>
        private void LoadGridPanel(string spantext, decimal gap)
        {
            gridPanel.Controls.Clear();
            if (gridPanel.InvokeRequired)
            {
                gridPanel.Invoke(new Action(() => LoadGridPanel(spantext, gap)));
                return;
            }
            gridPanel.Span = spantext;
            gridPanel.Gap = (int)gap;
            spantext = spantext.Substring(0, spantext.IndexOf('-'));
            // ; \n用于分割行   空格分割列
            // 50% 50%;20% 80%; 表示两行两列，值50%表示行宽
            string[] array = spantext.Split(new char[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 1; i < array.Length ; i++)
            {
                var control = new AntdUI.Button()
                {
                    Text = "Button" + i,
                    Type = TTypeMini.Primary,
                    WaveSize = 0,
                    Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
                };
                gridPanel.Controls.Add(control);
                control.BringToFront();
            }
        }

        private void input_span_TextChanged(object sender, EventArgs e)
        {
            LoadGridPanel(input_span.Text, 0);
        }

        private void LayoutForm_Load(object sender, EventArgs e)
        {
            LoadGridPanel(input_span.Text, 0);

            stackPanel1.AutoScroll = true;
            stackPanel1.Gap = 0;
            stackPanel1.ItemSize = "10%";
            stackPanel1.Vertical = true;
        }

        private void panel1_Click(object sender, EventArgs e)
        {

        }

        private void splitter1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button_Add2_Click(object sender, EventArgs e)
        {
            var control = new AntdUI.Button()
            {
                Margin = new Padding(0),
                Padding = new Padding(3),
                Text = $"Button{stackPanel1.Controls.Count + 1}",
                Type = TTypeMini.Primary,
                WaveSize = 0,
                Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134))),
            };
            stackPanel1.Controls.Add(control);
            control.BringToFront();
        }

        private void button_del2_Click(object sender, EventArgs e)
        {
            
            if (stackPanel1.Controls.Count > 0)
            {
                // 删除最后一个控件
                stackPanel1.Controls.RemoveAt(0);
            }
        }
    }
}
